package com.ruoyi.system.controller.person;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.dto.AttendanceDto;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.AttendanceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Attendance;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 出勤汇总表Controller
 *
 * @author ruoyi
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/attendance")
public class AttendanceController extends BaseController
{
    @Autowired
    private IAttendanceService attendanceService;
    @Autowired
    private ISysPostService postService;
    @Autowired
    private IEmplyeeService emplyeeService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询出勤汇总表列表
     */
    @RequiresPermissions("system:attendance:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AttendanceDto dto)
    {
//        System.out.println("查询dto:"+dto.getDateMonth());
        Attendance attendance = new Attendance();
        //根据员工姓名查询员工编号
        if(dto.getName()!=null){
            Emplyee emp = emplyeeService.selectEmplyeeByName(dto.getName());
            attendance.setEno(emp.getEno());
        }
        if(dto.getDateMonth()!=null) {
            attendance.setDate(new Date(dto.getDateMonth()));
        }
//        System.out.println("attendance:"+attendance.getDate());
        startPage();
        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
        List<AttendanceVo> listRes = new ArrayList<>();
        for (Attendance attend : list) {
            AttendanceVo attendanceVo = new AttendanceVo();
            BeanUtils.copyProperties(attend,attendanceVo);
            //设置员工名称
            Emplyee emplyee = emplyeeService.selectEmplyeeByEno(attend.getEno());
            attendanceVo.setName(emplyee.getName());
            //设置部门名称
            SysDept sysDept = deptService.selectDeptById(attend.getDeptId());
            attendanceVo.setDeptName(sysDept.getDeptName());
            //设置职称
            SysPost sysPost = postService.selectPostById(attend.getPostId());
            attendanceVo.setPosition(sysPost.getPostName());
            listRes.add(attendanceVo);
        }
        return getDataTable(listRes);
    }

    /**
     * 导出出勤汇总表列表
     */
    @RequiresPermissions("system:attendance:export")
    @Log(title = "出勤汇总表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attendance attendance)
    {
        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
        ExcelUtil<Attendance> util = new ExcelUtil<Attendance>(Attendance.class);
        util.exportExcel(response, list, "出勤汇总表数据");
    }

    /**
     * 获取出勤汇总表详细信息
     */
    @RequiresPermissions("system:attendance:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attendanceService.selectAttendanceById(id));
    }

    /**
     * 新增出勤汇总表
     */
    @RequiresPermissions("system:attendance:add")
    @Log(title = "出勤汇总表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Attendance attendance)
    {
        return toAjax(attendanceService.insertAttendance(attendance));
    }

    /**
     * 修改出勤汇总表
     */
    @RequiresPermissions("system:attendance:edit")
    @Log(title = "出勤汇总表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attendance attendance)
    {
        System.out.println("修改出勤："+attendance);
        return toAjax(attendanceService.updateAttendance(attendance));
    }

    /**
     * 删除出勤汇总表
     */
    @RequiresPermissions("system:attendance:remove")
    @Log(title = "出勤汇总表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceService.deleteAttendanceByIds(ids));
    }
}
